package com.konsung.ui.defineview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.konsung.bean.MeasureDataBean;
import com.konsung.bean.PatientBean;
import com.konsung.bean.PhysicalExaminationBean;
import com.konsung.bean.RecordAllMeasureResult;
import com.konsung.healthfile.R;
import com.konsung.ui.HealthReportActivity;
import com.konsung.util.IdCardUtil;
import com.konsung.util.KParamType;
import com.konsung.util.MeasureUtil;
import com.konsung.util.UiUtils;
import com.konsung.util.constant.GlobalConstant;

import java.util.Date;

/**
 * 类功能：
 */
public class PhysicalExaminationDrawable extends Drawable {

    private final int height;
    private final int width;
    Handler handler = new Handler();
    private MeasureDataBean measureBean;
    private PatientBean patientBean;
    private Paint paint;
    private RectF rect;
    private int urtConfig; //尿常规配置参数
    int deviceConfig; //设备配置参数
    int measureConfig; //测量项配置参数
    public static final String TITLE_COLOR = "#c3f1eb";
    public static final String TEXT_COLOR = "#333333";
    private int y; //绘制最后的Y坐标值
    RecordAllMeasureResult recordAllMeasureResult; //打印显示值类
    private Context context; //上下文引用
    /**
     * 构造器
     * @param context 上下文
     * @param measureBean 测量数据
     * @param patientBean 居民信息
     * @param width       宽度
     * @param height      高度
     */
    public PhysicalExaminationDrawable(Context context, MeasureDataBean measureBean
            , PatientBean patientBean, int width, int height) {
        this.height = height;
        this.width = width;
        this.measureBean = measureBean;
        this.patientBean = patientBean;
        this.context = context;
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        paint = new Paint();
        paint.setAntiAlias(true);
        canvas.drawColor(Color.TRANSPARENT);
        canvas.drawColor(Color.WHITE);
        recordAllMeasureResult = ((HealthReportActivity) context).getPrintResult();
        drawMeasureData(canvas);
    }

    private int measureResultX;
    private int measureResultY;
    private int rowInterval = 30;
    private int bitmapWidthwidth;
    private int offsetX1 = 230;
    private int offsetX2 = 200;
    private int offsetX3 = 190;
    private int offsetX4 = 196;
    private int titleSize = 32;
    private int tabSize = 24;
    private int contentSize = 22;
    private int lineSize = 2;
    private String tabLineColor = "#c3f2eb";
    private String lineColor = "#cccccc";
    private String titleLineColor = "#808080";

    /**
     * 设置界面显示数据
     *
     * @param measureBean 测量数据
     * @param patientBean 居民数据
     */
    public void setData(MeasureDataBean measureBean, PatientBean patientBean) {
        this.measureBean = measureBean;
        this.patientBean = patientBean;
        deviceConfig = measureBean.getParamValue();
        urtConfig = measureBean.getUrtConfig();
        measureConfig = measureBean.getMeasureValue();
    }

    /**
     * 绘制测量数据报告图
     *
     * @param canvas 画布
     */
    public void drawMeasureData(Canvas canvas) {
        try {
            int x; //x方向坐标
            int y; //y方向坐标
            //打印标题的起始位置
            final int titleX = 235;
            final int titleY = 60;
            //打印个人图标的起始位置
            final int persionPicX = 70;
            final int persionPicY = 135;
            //打印测量结果的起始位置
            measureResultX = 52;
            measureResultY = persionPicY + rowInterval * 2;

            // 画布宽度
            bitmapWidthwidth = width;

            //设置画笔
            //打印标题
            paint.setColor(Color.parseColor(TEXT_COLOR));
            paint.setTextSize(titleSize);
            paint.setStrokeWidth(4);
            x = 500;
            y = titleY + 35;
            canvas.drawText(UiUtils.getString(R.string.x_report), x, y,
                    paint);

            paint.setStrokeWidth((float) 2);
            paint.setColor(Color.parseColor(titleLineColor));
            canvas.drawLine(measureResultX, y + 20,
                    bitmapWidthwidth - measureResultX, y + 20,
                    paint);
//            //照片终点位置
//            int picEndX = 172;
//            int picEndY = 261;

            //第一行个人信息
            //打印个人信息的起始位置
            paint.setColor(Color.parseColor(TEXT_COLOR));
            paint.setTextSize(contentSize);
            final int persionInfoX = measureResultX + 75;
            final int persionInfoY = 175;
            x = persionInfoX;
            y = persionInfoY;
            canvas.drawText(getString(R.string.name1), x, y, paint);
            x += 75;
            if (patientBean.getName().length() > 9) {
                canvas.drawText(patientBean.getName().substring(0, 9) + "…", x, y, paint);
            } else {
                canvas.drawText(patientBean.getName(), x, y, paint);
            }
            x += 225;
            canvas.drawText(getString(R.string.sex1), x, y, paint);
            x += UiUtils.getDimen(R.dimen.px_small);
            canvas.drawText(IdCardUtil.getSexFormDB(patientBean
                    .getSex()), x, y, paint);

            x += 225;
            canvas.drawText(getString(R.string.physical_examination_time1), x, y, paint);
            x += UiUtils.getDimen(R.dimen.px_big);
            String measureTime = UiUtils.getDateFormat(UiUtils.DateState.LONG)
                    .format(new Date(measureBean.getMeasureTime()));
            canvas.drawText(measureTime, x, y, paint);

            //第二行个人信息
            x = persionInfoX;
            y = persionInfoY + 50;
            canvas.drawText(getString(R.string.age1), x, y, paint);
            x += 75;
            canvas.drawText("" + patientBean.getAge() + UiUtils.getString(R.string.year),
                    x, y, paint);

            x += 225;
            canvas.drawText(getString(R.string.doctor), x, y, paint);
            x += 75;
            canvas.drawText("" + measureBean.getDoctor(), x, y, paint);

//            x += 225;
//            Date date = new Date();
//            String printDate = UiUtils.getDateFormat(UiUtils.DateState.LONG).format(date);
//            canvas.drawText(getString(R.string.print_report), x, y, paint);
//            x += 120;
//            canvas.drawText("" + printDate, x, y, paint);

            //打印测量结果
            x = measureResultX;
            y += 35;
            //打印心率测量结果
            if (recordAllMeasureResult.isPrintEcg()) {
                y = drawFistTitle(x, y, canvas, KParamType.ECG_HR);
            }

            //打印血氧测量结果
            if (recordAllMeasureResult.isPrintBloodOx()) {
                y = drawFistTitle(x, y, canvas, KParamType.SPO2_TREND);
                //脉率测量结果
                y = drawSecondTitle(x, y, canvas, KParamType.SPO2_PR);
            }

            if (recordAllMeasureResult.isPrintBloodPress()) {
                //打印血压测量结果
                y = drawFistTitle(x, y, canvas, KParamType.NIBP_SYS);

                //舒张压测量结果
                y = drawSecondTitle(x, y, canvas, KParamType.NIBP_DIA);
            }

            //打印体温测量结果
            if (((deviceConfig & (0x01 << 4)) != 0 || (deviceConfig & (0x01 << 5)) != 0)
                    && recordAllMeasureResult.isPrintTemp()) {
                y = drawFistTitle(x, y, canvas, KParamType.IRTEMP_TREND);
            }

            //打印血糖测量结果 2三诺 6百捷单 7百捷三合一 8艾康 16倍稳
            if (((deviceConfig & (0x01 << 2)) != 0
                    || (deviceConfig & (0x01 << 6)) != 0
                    || (deviceConfig & (0x01 << 7)) != 0
                    || (deviceConfig & (0x01 << 8)) != 0
                    || (deviceConfig & (0x01 << 16)) != 0)
                    && recordAllMeasureResult.isPrintBloodGlu()) {
                //默认显示空腹
                if (measureBean.getTrendValue(KParamType.BLOODGLU_AFTER_MEAL)
                        != GlobalConstant.INVALID_DATA) {
                    y = drawFistTitle(x, y, canvas, KParamType.BLOODGLU_AFTER_MEAL);
                } else {
                    y = drawFistTitle(x, y, canvas, KParamType.BLOODGLU_BEFORE_MEAL);
                }
            }

            //打印尿常规测量结果 1恩普， 9优利特， 10艾康
            if (((deviceConfig & (0x01 << 1)) != 0
                    || (deviceConfig & (0x01 << 9)) != 0
                    || (deviceConfig & (0x01 << 10)) != 0)
                    && recordAllMeasureResult.isPrintUrint()) {
                y = drawFistTitle(x, y, canvas, KParamType.URINERT_LEU);
                y = drawSecondTitle(x, y, canvas, KParamType.URINERT_NIT);
                y = drawSecondTitle(x, y, canvas, KParamType.URINERT_UBG);
                y = drawSecondTitle(x, y, canvas, KParamType.URINERT_PRO);
                y = drawSecondTitle(x, y, canvas, KParamType.URINERT_PH);
                y = drawSecondTitle(x, y, canvas, KParamType.URINERT_SG);
                y = drawSecondTitle(x, y, canvas, KParamType.URINERT_BLD);
                y = drawSecondTitle(x, y, canvas, KParamType.URINERT_KET);
                y = drawSecondTitle(x, y, canvas, KParamType.URINERT_BIL);
                y = drawSecondTitle(x, y, canvas, KParamType.URINERT_GLU);
                y = drawSecondTitle(x, y, canvas, KParamType.URINERT_ASC);
                //是14的测量项配置
                if ((urtConfig & (0x01 << 2)) != 0) {
                    y = drawSecondTitle(x, y, canvas, KParamType.URINERT_ALB);
                    y = drawSecondTitle(x, y, canvas, KParamType.URINERT_CRE);
                    y = drawSecondTitle(x, y, canvas, KParamType.URINERT_CA);
                } else if ((urtConfig & (0x01 << 1)) != 0) {
                    //是11+2的测量项配置
                    y = drawSecondTitle(x, y, canvas, KParamType.URINERT_ALB);
                    y = drawSecondTitle(x, y, canvas, KParamType.URINERT_CRE);
                    y = drawSecondTitle(x, y, canvas, KParamType.URINERT_AC);
                }
            }

            //打印白细胞测量结果
            if (((deviceConfig & (0x01 << 11)) != 0) && recordAllMeasureResult.isPrintWbc()) {
                y = drawFistTitle(x, y, canvas, KParamType.BLOOD_WBC);
            }

            //打印血红蛋白测量结果
            if (((deviceConfig & (0x01 << 14)) != 0) && recordAllMeasureResult.isPrintHgb()) {
                y = drawFistTitle(x, y, canvas, KParamType.BLOOD_HGB);
                //打印红细胞压积值测量结果
                y = drawSecondTitle(x, y, canvas, KParamType.BLOOD_HCT);
            }

            //打印尿酸测量结果
            if (((deviceConfig & (0x01 << 7)) != 0)) {
                if (recordAllMeasureResult.isPrintUric()) {
                    y = drawFistTitle(x, y, canvas, KParamType.URICACID_TREND);
                }
                //打印总胆固醇测量结果
                if (recordAllMeasureResult.isPrintChol()) {
                    y = drawFistTitle(x, y, canvas, KParamType.CHOLESTEROL_TREND);
                }
            }

            //打印糖化血红蛋白测量结果
            if (((deviceConfig & (0x01 << 17)) != 0) && recordAllMeasureResult.isPrintGhb()) {
                y = drawFistTitle(x, y, canvas, KParamType.GHB_HBA1C_NGSP);

                y = drawSecondTitle(x, y, canvas, KParamType.GHB_HBA1C_IFCC);

                y = drawSecondTitle(x, y, canvas, KParamType.GHB_EAG);
            }

            //打印血脂测量结果
            if (((deviceConfig & (0x01 << 15)) != 0)
                    && recordAllMeasureResult.isPrintBloodFat()) {
                y = drawFistTitle(x, y, canvas, KParamType.BLOOD_FAT_CHO);

                y = drawSecondTitle(x, y, canvas, KParamType.BLOOD_FAT_TRIG);
                y = drawSecondTitle(x, y, canvas, KParamType.BLOOD_FAT_HDL);
                y = drawSecondTitle(x, y, canvas, KParamType.BLOOD_FAT_LDL);
            }

            if (recordAllMeasureResult.isPrintBmi()) {
                //打印BMI测量结果
                if ((measureConfig & (0x01 << 0)) != 0 && (measureConfig & (0x01 << 1)) != 0) {
                    y = drawFistTitle(x, y, canvas, KParamType.BMI);
                    y = drawSecondTitle(x, y, canvas, KParamType.HEIGHT);
                    y = drawSecondTitle(x, y, canvas, KParamType.WEIGHT);

                } else if ((measureConfig & (0x01 << 0)) != 0) {
                    y = drawFistTitle(x, y, canvas, KParamType.HEIGHT);

                } else if ((measureConfig & (0x01 << 1)) != 0) {
                    y = drawFistTitle(x, y, canvas, KParamType.WEIGHT);

                }
            }

            //打印结论与建议
//            x = measureResultX + 80;
//            y += 10;
//            drawReportTitle(getString(R.string.result), x, y, canvas, paint, false);
//            x = measureResultX;
//            y += 80;
//            paint.setStyle(Paint.Style.STROKE);
//            int adviceEndY = y + 250;
//            canvas.drawRect(x, y,
//                    bitmapWidthwidth - measureResultX, adviceEndY,
//                    paint);
//            this.y = adviceEndY;
            this.y = y;
        } catch (Exception e) {

        }

    }

    /**
     * 设置drawable的y轴值
     *
     * @param y y轴值
     */
    private void setY(int y) {
        this.y = y;
    }

    /**
     * 获取drawable的y轴值
     *
     * @return drawable的y轴值
     */
    public int getY() {

        return y;
    }

    /**
     * 根据id获取字符
     *
     * @param id id
     * @return 字符
     */
    private String getString(int id) {
        return UiUtils.getString(id);
    }

    /**
     * 画打印报告中的标题栏
     *
     * @param title  标题名
     * @param x      标题起始x
     * @param y      标题起始y
     * @param canvas 画布
     * @param paint  画笔
     * @param b      是否包含了副标题栏
     * @return 返回Y轴高度
     */
    private int drawReportTitle(String title, int
            x, int y, Canvas canvas, Paint paint, boolean b) {
        x = measureResultX;
        paint.setStrokeWidth((float) 1);
        paint.setColor(Color.parseColor(TITLE_COLOR));
        canvas.drawLine(x, y,
                bitmapWidthwidth - measureResultX, y,
                paint);
        y += 5;
        canvas.drawRect(x, y,
                bitmapWidthwidth - measureResultX, y + 48,
                paint);

        x += 40;
        y += 35;
        paint.setTextSize(tabSize);
        paint.setColor(Color.parseColor(TEXT_COLOR));
        canvas.drawText(title, x, y, paint);

        paint.setColor(Color.parseColor(TEXT_COLOR));

        if (b) {
            // subtitle 副标题栏
            paint.setTextSize(contentSize);
            y += 52;
            canvas.drawText(getString(R.string.item_name), x, y, paint);

            x += offsetX1;
            canvas.drawText(getString(R.string.check_result), x, y, paint);

            x += offsetX2;
            canvas.drawText(getString(R.string.hint), x, y, paint);

            x += offsetX3;
            canvas.drawText(getString(R.string.unit), x, y, paint);

            x += offsetX4;
            canvas.drawText(getString(R.string.reference_value), x, y, paint);

            paint.setStrokeWidth(lineSize);
            paint.setColor(Color.parseColor(lineColor));
            y += 20;
            canvas.drawLine(measureResultX, y,
                    bitmapWidthwidth - measureResultX, y,
                    paint);
        }
        return y;
    }

    /**
     * 绘制第一栏标题和内容
     *
     * @param x      x轴距离
     * @param y      y轴距离
     * @param canvas 画布
     * @param param  对应的参数值
     * @return 移动后的y轴距离
     */
    private int drawFistTitle(int x, int y, Canvas canvas, int param) {
        x = measureResultX;
        y += 20;
        PhysicalExaminationBean examinationBean = MeasureUtil.getPhysicalExaminationBean(param,
                measureBean);
        y = drawReportTitle(examinationBean.getTitleName(), x, y, canvas, paint, true);
        x += 40;
        y += 30;
        paint.setColor(Color.parseColor(TEXT_COLOR));
        canvas.drawText(examinationBean.getMeasureName(), x, y, paint);
        x += offsetX1;
        canvas.drawText(examinationBean.getMeasureValue(), x + 27, y, paint);
        x += offsetX2;
        canvas.drawText(examinationBean.getHint(), x, y, paint);
        x += offsetX3;
        canvas.drawText(examinationBean.getUnit(), x, y, paint);
        x += offsetX4;
        canvas.drawText(examinationBean.getScope(), x, y, paint);
        return y;
    }

    /**
     * 绘制第二栏标题和内容
     *
     * @param x      x轴距离
     * @param y      y轴距离
     * @param canvas 画布
     * @param param  对应的参数值
     * @return 移动后的y轴距离
     */
    private int drawSecondTitle(int x, int y, Canvas canvas, int param) {

        PhysicalExaminationBean examinationBean = MeasureUtil.getPhysicalExaminationBean(param,
                measureBean);
        x = measureResultX + 40;
        y += rowInterval;
        paint.setTextSize(contentSize);
        paint.setColor(Color.parseColor(TEXT_COLOR));
        canvas.drawText(examinationBean.getMeasureName(), x, y, paint);
        x += offsetX1;
        canvas.drawText(examinationBean.getMeasureValue(), x + 27, y, paint);
        x += offsetX2;
        canvas.drawText(examinationBean.getHint(), x, y, paint);
        x += offsetX3;
        //康尚尿机不需要绘制单位
        Boolean urtFlag = param == KParamType.URINERT_ALB
                || param == KParamType.URINERT_CA
                || param == KParamType.URINERT_CRE;
        if ((deviceConfig & (0x01 << 1)) != 0 && urtFlag) {
            canvas.drawText("/", x, y, paint);
        } else {
            canvas.drawText(examinationBean.getUnit(), x, y, paint);
        }
        x += offsetX4;
        canvas.drawText(examinationBean.getScope(), x, y, paint);
        return y;
    }

    @Override
    public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {

    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.UNKNOWN;
    }
}
